/**  
* @Title: FeignConfig.java 
* @Description:
* @Copyright: Copyright (c) 2018
* @Company:http://www.sinocon.cn
* @author Administrator  
* @date 2018年4月21日  
* @version 1.0  
*/
package com.sansui.config;

import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.RequestContext;

import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * @Title: FeignConfig
 * @Description:
 * @author:Administrator
 * @date 2018年4月21日
 */
@Configuration
public class FeignConfig implements RequestInterceptor {

	/*
	 * (non-Javadoc)
	 * 
	 * <p>Title: apply</p>
	 * 
	 * <p>Description: </p>
	 * 
	 * @param template y
	 * 
	 * @see feign.RequestInterceptor#apply(feign.RequestTemplate)
	 * 
	 */
	@Override
	public void apply(RequestTemplate template) {
//		template.header("shiroSessionId", getHeaders(getHttpServletRequest()).get("shiroSessionId"));
	}

	private HttpServletRequest getHttpServletRequest() {
		try {
			RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
			return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		} catch (Exception e) {
			return null;
		}
	}

	private Map<String, String> getHeaders(HttpServletRequest request) {
		Map<String, String> map = new LinkedHashMap<>();
		Enumeration<String> enumeration = request.getHeaderNames();
		while (enumeration.hasMoreElements()) {
			String key = enumeration.nextElement();
			String value = request.getHeader(key);
			map.put(key, value);
		}
		return map;
	}
}
